import Cookies from 'js-cookie'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const customizedAccount = ['d0558246878', 'd1066831142']

const adminLoginTokenKey = 'admin_login_token_monitor'
const adminLoginStatusKey = 'admin_login_status_monitor'
const adminLoginExpireKey = 'admin_login_expire_monitor'
const adminRefreshLoginTokenKey = 'admin_refresh_login_expire_monitor'
const agentTypeKey = 'admin_agent_type_monitor'
const userTypeKey = 'admin_user_type_monitor'
const accountNameKey = 'admin_account_name_monitor'

if(ACCOUNT_NAME && customizedAccount.includes(_ACCOUNT)) {
    localStorage.setItem(accountNameKey, ACCOUNT_NAME)
}else {
    ACCOUNT_NAME = ''
    localStorage.removeItem(accountNameKey)
}

var loginStatus = parseInt(sessionStorage.getItem(adminLoginStatusKey) || 0)

if(!loginStatus) {
    if(GetUrlParam('from') === 'platform') { //从平台来
        //从平台导入
        var loginExpire = Cookies.get('admin_login_expire') //登录时效
        var loginToken = localStorage.getItem('admin_login_token') //登录token
        var refreshToken = localStorage.getItem('Login-Refresh') //登录刷新token
        var userType = localStorage.getItem('admin_user_type') //用户类型
        var agentType = localStorage.getItem('admin_agent_type') //代理商类型
    
        Cookies.set(adminLoginExpireKey, loginExpire, {expires : new Date(loginExpire)})
        localStorage.setItem(adminLoginTokenKey, loginToken)
        localStorage.setItem(adminRefreshLoginTokenKey, refreshToken)
        localStorage.setItem(userTypeKey, userType)
        localStorage.setItem(agentTypeKey, agentType)
    }
}



if(!loginStatus) {
    var loginExpire = Cookies.get(adminLoginExpireKey) //登录时效
	if(loginExpire) {
        sessionStorage.setItem(adminLoginStatusKey, 1)
		loginStatus = 1
	}
}

let loginAccount = localStorage.getItem('login-account') || ''

const store = new Vuex.Store({
    state: {
        vehicleTypeList : {1 : '电动车', 2 : '充电柜', 3 : '换电柜', 4 : '汽车', electrocar : '电动车', chargeCabinet : '充电柜', exchangeCabinet : '换电柜', car : '汽车'},
        cabinetPortStatus : {0 : '空仓', 1 : '充电中', 2 : '充满', 5 : '异常'},
        menuList : [
            {title : '首页', name : 'index', to : '/', icon : require('@/assets/images/aside/home.png'), allow : [0, 1]},
            {title : '用户', name : 'user', to : '/user', icon : require('@/assets/images/aside/user.png'), allow : [0]},
            {title : '设备', name : 'device', to : '/device', icon : require('@/assets/images/aside/device.png'), allow : [0, 1]},
            {title : '订单', name : 'order', to : '/order', icon : require('@/assets/images/aside/order.png'), allow : [0, 1]}
        ],
        isBiguiyuan : loginAccount === 'd0558246878',
        loginAccount : loginAccount,
        loginToken : localStorage.getItem(adminLoginTokenKey) || '',
        refreshToken : localStorage.getItem(adminRefreshLoginTokenKey) || '',
        loginStatus : loginStatus,
        agentType : parseInt(localStorage.getItem(agentTypeKey) || 0), //代理商类型,1是特约代理商
        userType : parseInt(localStorage.getItem(userTypeKey) || 0), //用户类型 0:平台，1：代理商，2：门店

        accountName : ACCOUNT_NAME || (localStorage.getItem(accountNameKey) || '')
    },

    mutations: {
        setAccountName (state, data) {
            if(data && customizedAccount.includes(data.account)) {
                state.accountName = data.name
                localStorage.setItem(accountNameKey, data.name)
            }else {
                state.accountName = ''
                localStorage.removeItem(accountNameKey)
            }
            
        },

        setAgentType (state, agentType) {
            localStorage.setItem(agentTypeKey, agentType)
            state.agentType = agentType
        },

        setUserType (state, userType) {
            localStorage.setItem(userTypeKey, userType)
            state.userType = userType
        },

        setLoginStatus (state, loginStatus = 0) {
            state.loginStatus = loginStatus
            if(loginStatus == 1) {
                sessionStorage.setItem(adminLoginStatusKey, 1)
            }else {
                sessionStorage.removeItem(adminLoginStatusKey)
            }
        },

        setLoginExpire (state, loginExpire) {
            if(loginExpire > 0) {
                var time = new Date() * 1 + (loginExpire - 300) * 1000
                Cookies.set(adminLoginExpireKey, time, {expires : new Date(time)})
            }else {
                Cookies.remove(adminLoginExpireKey)
            }
        },

        setLoginToken (state, loginToken) {
            state.loginToken = loginToken
            if(loginToken !== '') {
                localStorage.setItem(adminLoginTokenKey, loginToken)
            }else {
                localStorage.removeItem(adminLoginTokenKey)
            }
        },

        setRefreshToken (state, refreshToken) {
            state.refreshToken = refreshToken
            if(refreshToken !== '') {
                localStorage.setItem(adminRefreshLoginTokenKey, refreshToken)
            }else {
                localStorage.removeItem(adminRefreshLoginTokenKey)
            }
        }
        
    },

    getters : {
        isPlatform : (state) => () => {
            return state.userType == 0
        },

        isAgent : (state) => () => {
            return state.userType == 1
        },

        isStore : (state) => () => {
            return state.userType == 2
        },
        
        isSite : (state) => () => {
            return state.userType == 3
        },

        hasPermission : (state) => (permission) => {
            if(state.isSuper == 1) {
                return true
            }
            return state.permission.includes(permission)
        },

        loginExpire : (state) => () => {
            return Cookies.get(adminLoginExpireKey) || 0
        }
    }
})

export default store